package com.xxx.dorm.service.impl;

import com.xxx.dorm.dao.BuildingDao;
import com.xxx.dorm.dao.DormManagerDao;
import com.xxx.dorm.dao.DormitoryDao;
import com.xxx.dorm.dao.StudentDao;
import com.xxx.dorm.dao.impl.BuildingDaoImpl;
import com.xxx.dorm.dao.impl.DormManagerDaoImpl;
import com.xxx.dorm.dao.impl.DormitoryDaoImpl;
import com.xxx.dorm.dao.impl.StudentDaoImpl;
import com.xxx.dorm.domain.Building;
import com.xxx.dorm.domain.Dormitory;
import com.xxx.dorm.service.BuildingService;

import java.util.List;

public class BuildingServiceImpl implements BuildingService {

    private BuildingDao buildingDao = new BuildingDaoImpl();
    private DormitoryDao dormitoryDao = new DormitoryDaoImpl();
    private StudentDao studentDao = new StudentDaoImpl();

    /**
     * 通过寝室表中的bid(即building表的id)查找宿舍楼信息
     * @param bid
     * @return
     */
    @Override
    public Building findByBid(Integer bid) {
        return buildingDao.findByBid(bid);
    }

    @Override
    public List<Building> findAll() {
        return buildingDao.findAll();
    }

    /**
     * 查找所有宿舍楼名
     * @return
     */
    @Override
    public List<Building> findName() {
        return buildingDao.findName();
    }

    @Override
    public List<Building> find(String conditions, String information) {
        return buildingDao.find(conditions,information);
    }

    @Override
    public void addBuild(Building building) {
        buildingDao.addBuild(building);
    }

    /**
     * 删除宿舍楼
     * @param id
     */
    @Override
    public void deleteBuild(Integer id) {
        Building building = buildingDao.findByBid(id);
        List<Dormitory> dormitories = dormitoryDao.findDormByBuildName(building.getName());
        for(int i = 0;i<dormitories.size();i++){
            studentDao.emptyDid(dormitories.get(i).getId());
        }
        buildingDao.deleteMiddleware(building.getId());
        dormitoryDao.deleteDormByBid(id);
        buildingDao.deleteBuild(id);
    }

    @Override
    public void updateBuild(Building building) {
        buildingDao.updateBuild(building);
    }
}
